package com.web.controller;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.web.model.BookInfo;
import com.web.service.BookService;


@Controller
public class IndexController {
	@Autowired
	private BookService bookService;
	@RequestMapping(value={"","index"})
	public String index(Model model){
		BookInfo bookInfo = new BookInfo();
		bookInfo.setIsBook(1);  //图书
		bookInfo.setStart(0);
		bookInfo.setLimit(20);
		List<BookInfo> books = bookService.findBooks(bookInfo);
		model.addAttribute("books", books);
		return "books";
	}
	@RequestMapping(value={"create"})
	public String create(Model model){
		return "create";
	}
	
	
	@RequestMapping(value={"index_"})
	public String index_(Model model){
		BookInfo bookInfo = new BookInfo();
		bookInfo.setIsBook(1);  //图书
		bookInfo.setStart(0);
		bookInfo.setLimit(6);
		List<BookInfo> books = bookService.findBooks(bookInfo);
		for (BookInfo book : books) {
			if(StringUtils.isNotBlank(book.getBookDesc()) && book.getBookDesc().length() > 300){
				book.setBookDesc(book.getBookDesc().substring(0, 300));
			}
		}
		model.addAttribute("books", books);
		return "index_";
	}
	
	@RequestMapping(value={"login"})
	public String login(Model model){
		
		return "login";
	}
}
